import { defineConfig, loadEnv, UserConfig, ConfigEnv } from 'vite'
import vue from '@vitejs/plugin-vue'
import AutoImport from 'unplugin-auto-import/vite'
import Components from 'unplugin-vue-components/vite'
import { resolve } from 'path'
// 配置gzip压缩插件
import viteCompression from 'vite-plugin-compression'
import UnoCSS from 'unocss/vite'

import Pages from 'vite-plugin-pages'
import Layouts from 'vite-plugin-vue-layouts'
import EnhanceLog from 'vite-plugin-enhance-log'
//css 自动加前缀和polyfill
import postcssPresetEnv from 'postcss-preset-env'
// https://vitejs.dev/config/
export default defineConfig(({ mode }: ConfigEnv): UserConfig => {
  const env = loadEnv(mode, process.cwd())
  const { VITE_PUBLIC_PATH, VITE_APP_PORT } = env
  return {
    base: VITE_PUBLIC_PATH,
    plugins: [
      vue(),
      UnoCSS(),
      AutoImport({
        // dts: './src/auto-imports.d.ts',
        dts: mode === 'development' ? 'src/typings/auto-imports.d.ts' : false,
        imports: [
          'vue',
          'vue-router',
          'pinia',
          '@vueuse/core',
          {
            // '@vueuse/core': [
            //   'computedAsync',
            //   'useVModel',
            //   ['get', '$get'],
            //   // ['useFetch', 'useMyFetch'], // import { useFetch as useMyFetch } from '@vueuse/core',
            // ],
          },
        ],
      }),
      Components({
        // resolvers: [IconsResolver(), ElementPlusResolver()],
        // 自动引入的后缀
        extensions: ['vue'],
        // allow auto import and register components used in markdown
        include: [/\.vue$/, /\.vue\?vue/, /\.md$/],
        dts: mode === 'development' ? 'src/typings/components.d.ts' : false, //生成声明文件位置
        // imports 指定组件所在位置，默认为 src/components
        dirs: ['src/common/components/', 'src/components'],
      }),
      Pages({
        pagesDir: 'src/views', //需要生成路由的文件目录，默认就是识别src下面的pages文件
        extensions: ['vue'],
        exclude: ['**/components/*.vue'], // 忽略的文件夹
        importMode: 'async', // 是否是异步路由
      }),
      Layouts({
        // 如果是默认 layouts文件夹，默认 default.vue文件，则不需要配置
        layoutsDirs: 'src/layouts', // 布局文件存放目录
        defaultLayout: 'default', //对应 src/layouts/default.vue
      }),
      EnhanceLog({
        splitBy: '\n',
        preTip: '🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥',
        enableFileName: true,
        endLine: true,
      }),
      viteCompression(), // 配置gzip压缩插件
    ],
    build: {
      target: 'es2015',
      sourcemap: mode === 'development',
    },
    css: {
      postcss: {
        plugins: [postcssPresetEnv()],
      },
      // CSS 预处理器
      preprocessorOptions: {
        // 定义全局 SCSS 变量
        scss: {
          javascriptEnabled: true,
          additionalData: `@use "@/styles/variables.scss" as *; `,
        },
      },
    },
    resolve: {
      alias: [
        {
          find: '@',
          replacement: resolve(__dirname, './src'),
        },
      ],
      // 情景导出 package.json 配置中的exports字段
      conditions: [],
      // 导入时想要省略的扩展名列表
      // 不建议使用 .vue 影响IDE和类型支持
      extensions: ['.mjs', '.js', '.ts', '.jsx', '.tsx', '.json'],
    },
    server: {
      headers: {
        'Access-Control-Allow-Origin': '*',
      },
      host: true,
      port: +VITE_APP_PORT,
      open: false, // 是否服务启动时自动打开浏览器
      cors: true, // 是否允许跨域
      proxy: {},
    },
  }
})
